using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;

public partial class WebUserControlstockselect : System.Web.UI.UserControl
{
    private SqlConnection _oleConn;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Common.stkCodeValue = 0;
            //Common.stkTypeValue = 0;
            DDList_Stype.SelectedIndex = 0;
            DDList_Stype_SelectedIndexChanged(null, null);
            //Common.stkTypeValue = Convert.ToInt32(DDList_Stype.SelectedValue);
        }
    }

    protected void DDList_Sitem_SelectedIndexChanged(object sender, EventArgs e)
    {
        Common.stkCodeValue = int.Parse(DDList_Sitem.SelectedValue.ToString());
        Common.stkCodeName = DDList_Sitem.SelectedItem.Text;

        DataSet ds = new DataSet();
        Common.Getdata(ds, "select acctype,acccode from stock where itype=" + Common.stkTypeValue + " and icode=" + Common.stkCodeValue, "acc");
        if (ds.Tables["acc"].Rows.Count > 0)
        {
            if (ds.Tables["acc"].Rows[0]["acccode"] != DBNull.Value)
            {
                Common.stkAccCode = Convert.ToInt32(ds.Tables["acc"].Rows[0]["acccode"]);
                Common.stkAccType = Convert.ToInt32(ds.Tables["acc"].Rows[0]["acctype"]);
            }
            else
            {
                Common.stkAccCode = 0;
                Common.stkAccType = 0;
            }
        }
    }

    protected void DDList_Stype_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string con = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            //string con = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
            StringBuilder sv = new StringBuilder(con);

            if (DDList_Stype.SelectedValue.Length > 0)
            {
                //TextBox1.Text = DDList_Stype.SelectedItem.Text.Trim();
                //TextBox1.Text = DDList_Stype.SelectedValue.ToString();
                Common.stkTypeValue = int.Parse(DDList_Stype.SelectedValue.ToString());
                Common.stkTypeName = DDList_Stype.SelectedItem.Text.Trim();
                _oleConn = new SqlConnection();
                _oleConn.ConnectionString = sv.ToString();
                string cmd1 = "SELECT INAME,ICODE,PRICE,ACCTYPE,ACCCODE FROM STOCK WHERE ITYPE=" + DDList_Stype.SelectedValue.ToString() + " and ITYPE != '' order by INAME asc";

                SqlCommand comm = new SqlCommand(cmd1, _oleConn);

                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();

                da.SelectCommand = comm;
                da.Fill(ds, "stk");

                // Due to some error in previous entry there is possibility that some of the inputs are NULL
                // that is itype exists but there is no item
                if (ds.Tables["stk"].Rows.Count > 0)
                {
                    DDList_Sitem.Enabled = true;
                    DDList_Sitem.DataSource = ds.Tables[0];
                    DDList_Sitem.DataBind();
                    //if ((ds.Tables[0].Rows[DDList_Sitem.SelectedIndex]["ACCCODE"] != DBNull.Value) && (ds.Tables[0].Rows[DDList_Sitem.SelectedIndex]["ACCTYPE"] != DBNull.Value))
                    //{
                    //}

                    DDList_Sitem.SelectedIndex = 0;
                    DDList_Sitem_SelectedIndexChanged(null, null);
                }
                else
                {
                    //DDList_Sitem.Enabled = false;
                    //DDList_Sitem.DataSource = null;
                    DDList_Sitem.Items.Clear();
                    DDList_Sitem.DataBind();
                    Common.stkCodeName = "";
                    Common.stkCodeValue = 0;
                    Common.stkPrice = 0;
                }

            }
        }
        catch (Exception Ex)
        {
            
            throw;
        }

    }

}
